package com.saunter.service;

import com.saunter.extendMapper.JobExtendMapper;
import com.saunter.extendModel.JobExtend;
import com.saunter.extendModel.UserExtend;
import com.saunter.mapper.JobMapper;
import com.saunter.model.Job;
import com.saunter.service.user.UserService;
import com.saunter.utils.exception.ExceptionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("jobService")
public class JobService {
    @Autowired
    private JobExtendMapper jobExtendMapper;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UserService userService;
    @Resource
    private ExceptionManager exceptionManager;

    public Job getById(int job_id) {
        return jobExtendMapper.selectByPrimaryKey(job_id);
    }

    public int add(Job job) {
        int department_id = job.getDepartmentId();
        Integer status = job.getStatus();
        if(null == departmentService.getById(department_id)) {
            throw exceptionManager.create("Dep001");
        }
        if(null != status && 0 != status && 1 != status) {
            throw exceptionManager.create("Prm003");
        }else if(null != jobExtendMapper.getByDepartmentAndName(department_id, job.getName())){
            throw exceptionManager.create("Job002");
        }else {
            return jobExtendMapper.insertSelective(job);
        }
    }

    public JobExtend getDepartmentJob(int job_id) {
        return jobExtendMapper.getDepartmentJob(job_id);
    }

    public int editName(Job new_job) {
        if(null == this.getById(new_job.getId())) {
            throw exceptionManager.create("Job001");
        }else{
            return jobExtendMapper.updateByPrimaryKeySelective(new_job);
        }
    }

    public int delete(int job_id) {
        if(null == this.getById(job_id)) {
            throw exceptionManager.create("Job001");
        }else {
            if(userService.getUserByJob(job_id).size() == 0) {
                return jobExtendMapper.deleteByPrimaryKey(job_id);
            }else {
                throw exceptionManager.create("Job003");
            }
        }
    }
}
